package com.busibuilding.codemstr.action;

import java.util.List;

import org.displaytag.tags.TableTagParameters;

import com.busibuilding.codemstr.services.CodeMstrService;
import com.busibuilding.codemstr.vo.CodeMstrCVO;
import com.busibuilding.common.action.CommonActionImpl;
import com.busibuilding.utils.StringUtil;

/**
 * Class Name: CodeMstrAction.java
 * Description: 
 * @author Lin Yanjun
 * @version 1.0
 * @since 1.0
 * Date: 2010-4-21
 */
public class CodeMstrAction extends CommonActionImpl {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7635376768911436451L;
	
	private boolean showAdd;
	private boolean showUpdate;
	private boolean showTable;
	
	private CodeMstrForm codeMstrForm = new CodeMstrForm(); 
	
	private int page;
	private int currentPage;
	
	private CodeMstrService service;

	public String search() throws Exception{
		CodeMstrCVO codeMstrCVO = new CodeMstrCVO();
		if(StringUtil.isNotEmpty(codeMstrForm.getCode())){
			codeMstrCVO.setCode(codeMstrForm.getCode());
		}
		if(StringUtil.isNotEmpty(codeMstrForm.getCodeName())){
			codeMstrCVO.setCodeName(codeMstrForm.getCodeName());
		}
		if(StringUtil.isNotEmpty(codeMstrForm.getCodeCategory())){
			codeMstrCVO.setCodeCategory(codeMstrForm.getCodeCategory());
		}
		if(StringUtil.isNotEmpty(codeMstrForm.getCodeDesc())){
			codeMstrCVO.setCodeDesc(codeMstrForm.getCodeDesc());
		}
		if(StringUtil.isNotEmpty(codeMstrForm.getDefunctInd())){
			codeMstrCVO.setDefunctInd(codeMstrForm.getDefunctInd());
		}else{
			codeMstrCVO.setDefunctInd("N");
		}
		codeMstrCVO.setMinIndex((getPage()-1)*10);
		codeMstrCVO.setMaxIndex(getPage()*10);
		int listSize = service.getRecordsCount(codeMstrCVO);
		List<CodeMstrCVO> resultList = service.searchCodemstrByCondition(codeMstrCVO);
		this.getRequest().setAttribute("codeMstrListSize", listSize);
		this.getRequest().setAttribute("codeMstrDisplayList", resultList);
		showTable = true;
		return INPUT;
	}
	
	public String del() throws Exception{
		CodeMstrCVO codeMstrCVO = new CodeMstrCVO();
		codeMstrCVO.setCodeMstrId(codeMstrForm.getCodeMstrId());
		codeMstrCVO.setDefunctInd(YES);
		getAdminCommonInformationForUpdate(codeMstrCVO);
		int code = service.deleteCodeMstr(codeMstrCVO);
		if(code==SQLMAP_COMMON_RESULT_ERROR){
			txnFailure(TRANS_SUCCESSFUL_MSG);
		}else{
			txnSuccessful(TRANS_SUCCESSFUL_MSG);
		}
		return "toSearchCodeMstr";
	}
	
	public String inputCreate() throws Exception{
		showAdd = true;
		showUpdate = false;
		showTable = false;
		return INPUT;
	}
	
	public String createCodeMstr() throws Exception{
		if (hasErrors()) {
			return INPUT;
		}
		CodeMstrCVO codeMstrCVO = new CodeMstrCVO();
		codeMstrCVO.setCode(codeMstrForm.getCode());
		List<CodeMstrCVO> resultList = service.searchCodemstrByCondition(codeMstrCVO);
		if(resultList.size()>0){
			addActionError(getText("general.message.rectifyInRed"));
			addFieldError("codeMstrForm.code", getText(
					"error.admin.codemstr.existCode",
					new String[] { codeMstrForm.getCode() }));
			codeMstrForm.setCode(EMPUT_STRING);
			return INPUT;
		}
		codeMstrCVO.setCodeName(codeMstrForm.getCodeName());
		codeMstrCVO.setCodeCategory(codeMstrForm.getCodeCategory());
		codeMstrCVO.setCodeDesc(codeMstrForm.getCodeDesc());
		getAdminCommonInformationForCreate(codeMstrCVO);
		service.createCodeMstr(codeMstrCVO);
		return "toSearchCodeMstr";
	}
	
	public String inputUpdate() throws Exception{
		CodeMstrCVO codeMstrCVO = new CodeMstrCVO();
		codeMstrCVO.setCodeMstrId(codeMstrForm.getCodeMstrId());
		codeMstrCVO = (CodeMstrCVO) service.getCodeMstr(codeMstrCVO);
		codeMstrForm.setCode(codeMstrCVO.getCode());
		codeMstrForm.setCodeName(codeMstrCVO.getCodeName());
		codeMstrForm.setCodeCategory(codeMstrCVO.getCodeCategory());
		codeMstrForm.setCodeDesc(codeMstrCVO.getCodeDesc());
		showAdd = false;
		showUpdate = true;
		showTable = false;
		return INPUT;
	}
	
	public String updateCodeMstr() throws Exception{
		if (hasErrors()) {
			return INPUT;
		}
		CodeMstrCVO tempCodeMstrCVO = new CodeMstrCVO();
		tempCodeMstrCVO.setCode(codeMstrForm.getCode());
		List<CodeMstrCVO> resultList = service.searchCodemstrByCondition(tempCodeMstrCVO);
		if(resultList.size()>1){
			addActionError(getText("general.message.rectifyInRed"));
			addFieldError("codeMstrForm.code", getText(
					"error.admin.codemstr.existCode",
					new String[] { codeMstrForm.getCode() }));
			codeMstrForm.setCode(EMPUT_STRING);
			return INPUT;
		}
		CodeMstrCVO codeMstrCVO = new CodeMstrCVO();
		codeMstrCVO.setCodeMstrId(codeMstrForm.getCodeMstrId());
		codeMstrCVO = (CodeMstrCVO) service.getCodeMstr(codeMstrCVO);
		codeMstrCVO.setCode(codeMstrForm.getCode());
		codeMstrCVO.setCodeName(codeMstrForm.getCodeName());
		codeMstrCVO.setCodeCategory(codeMstrForm.getCodeCategory());
		codeMstrCVO.setCodeDesc(codeMstrForm.getCodeDesc());
		getAdminCommonInformationForUpdate(codeMstrCVO);
		codeMstrCVO.setVersion(codeMstrCVO.getVersion()+1);
		service.updateCodeMstr(codeMstrCVO);
		return "toSearchCodeMstr";
	}
	
	public boolean isShowAdd() {
		return showAdd;
	}

	public void setShowAdd(boolean showAdd) {
		this.showAdd = showAdd;
	}

	public boolean isShowUpdate() {
		return showUpdate;
	}

	public void setShowUpdate(boolean showUpdate) {
		this.showUpdate = showUpdate;
	}

	public boolean isShowTable() {
		return showTable;
	}

	public void setShowTable(boolean showTable) {
		this.showTable = showTable;
	}

	public CodeMstrForm getCodeMstrForm() {
		return codeMstrForm;
	}

	public void setCodeMstrForm(CodeMstrForm codeMstrForm) {
		this.codeMstrForm = codeMstrForm;
	}

	public int getPage() {
		if (getRequest().getParameter(TableTagParameters.SORT_AMOUNT_PAGE) == null || "".equals(getRequest().getParameter(TableTagParameters.SORT_AMOUNT_PAGE))) {
        	page = currentPage;
        }else{
        	page = Integer.parseInt(getRequest().getParameter(TableTagParameters.SORT_AMOUNT_PAGE));
        }
		
		if (page == 0){
			page = DEFAULT_PAGE;
		}
		
		return page;
	}

	public void setPage(int page) {
		if (page > 0){
			this.page = page;
		}
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setService(CodeMstrService service) {
		this.service = service;
	}

}
